<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UserController extends Controller
{
  /**
   * 展示添加界面
   * @return page 会员添加界面
   */
  public function getAdd()
  {
    return view('admin.user.useradd');
  }

  /**
   * post提交过来的数据
   * @param  Request $req [Request对象]
   * @return [script] 成功提示添加成功 失败提示添加失败
   */
  public function postAdd(Request $req)
  {
    $user_salt='qwadas';
    $user_password=md5($user_salt."123456");
    //用户表
    $user=[
      'user_name'=>$req->user_name,
      'user_phone'=>$req->user_phone,
      'user_password'=>$user_password,
      'user_salt'=>$user_salt,
      'user_nick'=>$req->user_nick,
      'user_status'=>2,
      'regtime'=>time(),
    ];
    $uid=DB::table('users')->insertGetId($user);

    //用户游戏表
    $usergame=[
        'uid'=>$uid,
        'user_meiqiu'=>$req->user_meiqiu,
        'user_level'=>$req->user_level,
        'user_exp'=>$req->user_exp,
    ];
    $game=DB::table('user_games')->insert($usergame);

    //用户扩展表
    $userexts=[
      'uid'=>$uid,
      'user_email'=>$req->user_email,
      'user_phone'=>$req->user_phone,
      'user_question'=>$req->user_question,
      'user_answer'=>$req->user_answer,
      'user_truename'=>$req->user_truename,
      'user_idcard'=>$req->user_idcard,
    ];
    $exts=DB::table('user_exts')->insert($userexts);
    if($uid && $game && $exts){
        return '<script language="javascript">
                alert("添加成功");
                location.replace(document.referrer);
               </script>';
    }else{
        return '<script language="javascript">
                alert("添加失败");
                location.replace(document.referrer);
                </script>';
    }
    
  }

  /**
   * 查询所有会员
   * @return array [返回一个数组到会员列表页]
   */
  public function getList() 
  {

    $user=DB::table('users')->select('user_id','user_name','regtime','user_status')->where('user_status','!=','0')->get();
    $user_exts=DB::table('user_exts')->select('uid','user_email')->get();
    $user_games=DB::table('user_games')->select('uid','user_meiqiu','user_level')->get();
    return view('admin.user.userlist',['user'=>$user,'user_exts'=>$user_exts,'user_games'=>$user_games]);
  }

  /**
   * 封停账号
   * @param  [int] $user_id [用户id]
   * @return [script] 成功提示封号成功 失败提示封号失败
   */
  public function getFenghao($user_id)
  {
    $fenghao=DB::table('users')->where('user_id',$user_id)->update(array('user_status'=>3));
    if ($fenghao) {
      return '<script language="javascript">
                alert("封号成功");
                location.replace(document.referrer);
              </script>';
    } else {
      return '<script language="javascript">
                alert("封号失败");
                location.replace(document.referrer);
              </script>';
    }
  }
  /**
   * 解封账号
   * @param  [int] $user_id [用户id]
   * @return [script] 成功提示删除成功 失败提示删除失败
   */
  public function getJiefeng($user_id)
  {
    $jiefeng=DB::table('users')->where('user_id',$user_id)->update(array('user_status'=>2));
    if ($jiefeng) {
      return '<script language="javascript">
                alert("解封成功");
                location.replace(document.referrer);
              </script>';
    } else {
      return '<script language="javascript">
                alert("解封失败");
                location.replace(document.referrer);
              </script>';
    }

  }
  /**
   * 删除账号
   * @param  [int] $user_id [用户id]
   * @return [script] 成功提示删除成功 失败提示删除失败
   */
  public function getDel($user_id)
  {
    $del=DB::table('users')->where('user_id',$user_id)->update(array('user_status'=>0));
    if($del){
      return '<script language="javascript">
                alert("删除成功");
                location.replace(document.referrer);
              </script>';
    } else {
      return '<script language="javascript">
                alert("删除失败");
                location.replace(document.referrer);
              </script>';
    }
    return "id传递到del方法中";
  }

  /**
   * 修改会员时的会员查询
   * @param  [int] $user_id [用户id]
   * @return page  文章修改页面
   */
  public function getEdit($user_id)
  {  
    $user=DB::table('users')->select('user_id','user_name','user_phone','user_nick')->where('user_id',$user_id)->first();
    $user_exts=DB::table('user_exts')->select('user_email','user_question','user_answer','user_truename','user_idcard')->where('uid',$user_id)->first();
    $user_games=DB::table('user_games')->select('user_meiqiu','user_level','user_exp')->where('uid',$user_id)->first();
    return view('admin.user.useredit',['user'=>$user,'user_exts'=>$user_exts,'user_games'=>$user_games]);
  }

  /**
   * post过来的修改数据
   * @param  Request $req    [Request对象]
   * @param  [int]  $user_id [用户id]
   * @return [script] 成功提示修改成功 失败提示修改失败
   */
  public function postEdit(Request $req,$user_id)
  {
    //用户表
    $user=[
      'user_name'=>$req->user_name,
      'user_phone'=>$req->user_phone,
      'user_nick'=>$req->user_nick,
    ];
    
    //用户游戏表
    $usergame=[
      'user_meiqiu'=>$req->user_meiqiu,
      'user_level'=>$req->user_level,
      'user_exp'=>$req->user_exp,
    ];
    
    //用户扩展表
    $userexts=[
      'user_email'=>$req->user_email,
      'user_phone'=>$req->user_phone,
      'user_question'=>$req->user_question,
      'user_answer'=>$req->user_answer,
      'user_truename'=>$req->user_truename,
      'user_idcard'=>$req->user_idcard,
    ];
    $useredit=DB::table('users')->where('user_id',$user_id)->update($user);
      $gameedit=DB::table('user_games')->where('uid',$user_id)->update($usergame);
        $extsedit=DB::table('user_exts')->where('uid',$user_id)->update($userexts);
        if($extsedit && $gameedit && $useredit){

          return '<script language="javascript">
                alert("修改成功");
                location.replace(document.referrer);
                 </script>';
        }else{

          return '<script language="javascript">
                alert("修改失败");
                location.replace(document.referrer);
                  </script>';
    }
  }
}
